package com.tavenli.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tavenli.entity.DeviceCmdEntity;
import com.tavenli.entity.F18Device;
import com.tavenli.entity.UserEntity;
import com.tavenli.model.PageData;
import com.tavenli.services.DeviceCmdService;
import com.tavenli.services.IClockService;
import com.tavenli.utils.PageNavUtil;
import com.tavenli.utils.ValidatorUtil;

@Controller
@RequestMapping("/dev")
public class UDeviceController {
	private static Logger logger = LoggerFactory.getLogger(UCenterController.class);
	@Autowired
	private IClockService clockService;
	@Autowired
	private DeviceCmdService deviceCmdService;
	
	@RequestMapping("/devices")
	public String devicesList(Model model,Integer page,String userName) {
		page = page== null ? 1 : page<1 ? 1 : page;
		int pageSize = 10;
		PageData<F18Device> pageData = this.clockService.getF18Devices(page, pageSize, userName);
		model.addAttribute("dataList", pageData.getPageData());
		model.addAttribute("totalCount", pageData.getTotalCount());
		model.addAttribute("totalPage", pageData.getTotalPage());
		model.addAttribute("currentPage", page);
		model.addAttribute("pageNav", PageNavUtil.getPageNavHtml(page.intValue(), pageSize, pageData.getTotalCount(), 15));
		return "ucenter/dev/device_list";
	}
	
	@RequestMapping("/cmd_list")
	public String DeviceCmdList(Model model,Integer page,String sn) {
		page = page== null ? 1 : page<1 ? 1 : page;
		int pageSize = 10;
		PageData<DeviceCmdEntity> pageData = this.deviceCmdService.getDeviceCmdList(page, pageSize, sn);
		model.addAttribute("dataList", pageData.getPageData());
		model.addAttribute("totalCount", pageData.getTotalCount());
		model.addAttribute("totalPage", pageData.getTotalPage());
		model.addAttribute("currentPage", page);
		model.addAttribute("pageNav", PageNavUtil.getPageNavHtml(page.intValue(), pageSize, pageData.getTotalCount(), 15));
		return "ucenter/dev/cmd_list";
	}
	
	
	
	
	
	@RequestMapping("/deviceEdit")
	public String deviceEdit(Model model,int id) {
		F18Device f18Device = this.clockService.getF18DeviceById(id);
		model.addAttribute("f18Device", f18Device);
		return "ucenter/dev/device_edit";
	}
	@RequestMapping(value = "/saveF18Device", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveF18Device(Model model,F18Device f18Device) {
		boolean resultStatus = true;
		Map<String, Object> resMap = new HashMap<String, Object>();
		
		String deviceName = f18Device.getDeviceName();
		if(f18Device==null || StringUtils.isBlank(deviceName)){
			resultStatus = false;
			resMap.put("errorMsg", "设备名称不能为空");
			resMap.put("resultStatus", resultStatus);
			return resMap;
		}
		//过滤非法字符
		deviceName = ValidatorUtil.filterUnSafeChar(deviceName).trim();
		if (null!=f18Device&&f18Device.getId()>0) {
			f18Device=clockService.getF18DeviceById(f18Device.getId());
		}
		f18Device.setDeviceName(deviceName);
		resultStatus = this.clockService.regF18Device(f18Device);
		resMap.put("resultStatus", resultStatus);
		return resMap;
	}
	
	@RequestMapping(value = "/delDevice", method = RequestMethod.POST)
	@ResponseBody
	public int delDevice(int id) {
		//删除单个
		int resultCode = clockService.delDevice(id);
		return resultCode;
	}
	
	@RequestMapping(value = "/delDevices", method = RequestMethod.POST)
	@ResponseBody
	public int delDevices(Integer[] deviceIds) {
		//批量删除
		int resultCode = clockService.delDevices(deviceIds);
		return resultCode;
	}
	
	/**
	 * 实时监控页
	 * @param model
	 * @param page
	 * @param userName
	 * @return
	 */
	@RequestMapping("/device_rt")
	public String devicesRtList(Model model,String devSn,String devName) {
		PageData<F18Device> pageData=clockService.getF18Devices(0, 100, "");
		model.addAttribute("pageData", pageData);
		return "admin/device_rt";
	}
	
}
